package nc.bs.excel.translator;

import org.apache.commons.lang3.StringUtils;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.bd.accessor.GeneralAccessorFactory;
import nc.pubitf.bd.accessor.IGeneralAccessor;
import nc.vo.bd.accessor.IBDData;
import nc.vo.pfxx.exception.PfxxException;
import nccloud.bs.excel.ITranslateContext;
import nccloud.bs.excel.ITranslatorStrategy;
import nccloud.bs.excel.TranslateContext;
import nccloud.vo.excel.util.PfxxUtils;

public class OrgVTranslator implements ITranslatorStrategy {

	@Override
	public String translateExToNC(String srcValue, String metaDataID, ITranslateContext translateContext)
			throws PfxxException {
		IGeneralAccessor accessor = GeneralAccessorFactory.getAccessor(metaDataID);
		IBDData doc = null;
		doc = accessor.getDocByCode(getPk_org(translateContext), srcValue);
		if (doc == null) {
			doc = accessor.getDocByNameWithMainLang(getPk_org(translateContext), srcValue);
		}
		if (doc != null) {
			String pk = doc.getPk();
			String sql = "select pk_org from org_orgs_v where pk_vid = ?";
			SQLParameter param = new SQLParameter();
			param.addParam(pk);
			try {
				String pk_org = (String)new BaseDAO().executeQuery(sql, param, new ColumnProcessor());
				if(StringUtils.isNotEmpty(pk_org)) {
					PfxxUtils.getCurrentContext().getXChangeConfigInfo().setPk_org(pk_org);
				}
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return doc.getPk();
		}
		return null;
	}

	public String getPk_org(ITranslateContext translateContext) {
		return InvocationInfoProxy.getInstance().getGroupId();
	}

	@Override
	public String getStrategyDescription() {
		// TODO Auto-generated method stub
		return null;
	}

}
